package com.mhkj.execute.adjust.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mhkj.base.busiMode.service.BusiModelService;
import com.mhkj.base.busiType.service.BusinessTypeService;
import com.mhkj.base.itemInfo.item.service.ItemService;
import com.mhkj.execute.adjust.entity.PlanNodeAdjust;
import com.mhkj.execute.adjust.service.PlanNodeAdjustService;
import com.mhkj.execute.plan.service.PlanNodeExecutionService;
import com.mhkj.revise.plan.entity.PlanNode;
import com.mhkj.revise.plan.service.PlanNodeService;
import com.mics.core.common.componet.JsonHandler;
import com.mics.core.common.componet.Pagination;
import com.mics.core.common.componet.ResponseMessage;
import com.mics.core.system.base.controller.BaseController;
import com.mics.core.system.organizational.service.OrgService;
import com.mics.core.system.rbac.service.DictionaryService;

/**
 * 计划节点调整
 * 
 * @author wk
 * @date 2018年8月8日
 * @version 1.0
 */
@Controller
@RequestMapping("/planNodeAdjust")
public class PlanNodeAdjustController extends BaseController<PlanNodeAdjust> {
	@Resource
	private PlanNodeAdjustService planNodeAdjustService;
	@Resource
	private PlanNodeExecutionService planNodeExecutionService;
	@Resource
	private PlanNodeService planNodeService;
	@Resource
	private BusinessTypeService businessTypeService;
	@Resource
	private ItemService itemService;
	@Resource
	private DictionaryService dictionaryService;
	@Resource
	private OrgService orgService;
	@Resource
	private BusiModelService busiModelService;
	@Resource
	private JsonHandler jsonHandler;

	@Resource
	public void setPlanNodeAdjustService(PlanNodeAdjustService planNodeAdjustService) {
		super.setBaseService(planNodeAdjustService);
	}

	@InitBinder
	protected void init(HttpServletRequest request, ServletRequestDataBinder binder) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		dateFormat.setLenient(false);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
	}

	/**
	 * 
	 * @param response
	 * @param planNode
	 * @param pageIndex
	 * @param pageSize
	 */
	@RequestMapping("/listByPage")
	@ResponseBody
	public void listByPage(HttpServletResponse response, PlanNode planNode, int pageIndex, int pageSize) {
		Pagination<PlanNode> pagination = planNodeService.listByPage(planNode, pageIndex, pageSize);
		jsonHandler.sendJsonString(response, true, "获取数据成功", pagination,
				new String[] { "plan.name", "nodeName", "business.typeName", "level", "taskType.value", "completeStandard",
						"beforeCondition", "beginDate", "endDate", "deadline", "officeName", "managerName",
						"evaluateName","rate","state","remark" },
				new String[] { "planName", "nodeName", "businessName", "level", "taskTypeValue", "completeStandard",
						"beforeCondition", "beginDate", "endDate", "deadline", "officeName", "managerName",
						"evaluateName","rate","state","remark" });
	}
	
	/**
	 * 受影响的节点
	 * @param response
	 * @param planNode
	 * @param pageIndex
	 * @param pageSize
	 */
	@RequestMapping("/affectedListByPage")
	@ResponseBody
	public void affectedListByPage(HttpServletResponse response, PlanNode planNode, int pageIndex, int pageSize) {
		
		
		PlanNode node =planNodeService.getById(planNode.getId());
		
		Pagination<PlanNode> pagination = planNodeExecutionService.affectedListByPage(node, pageIndex, pageSize);
		/*jsonHandler.sendJsonString(response, true, "获取数据成功", pagination,
				new String[] { "plan.name", "nodeName", "business.typeName", "completeStandard", "beginDate", "endDate", "deadline", "officeName", "managerName",
						"rate","state","remark" },
				new String[] { "planName", "nodeName", "businessName", "completeStandard","beginDate", "endDate", "deadline", "officeName", "managerName",
						"rate","state","remark" });*/
		
		

		jsonHandler.sendJsonString(response, true, "获取数据成功", pagination,
				new String[] { "id","plan.name","business.typeName","plan.planType.value","nodeName","nodeProperty.value","completeStandard","deadlineDesc","rate","state","beginDate","endDate","postName","remark" },
				new String[] { "id","planName","businessName","planTypeName","nodeName","nodePropertyName","completeStandard","deadlineDesc","rate","state","beginDate","endDate","postName","remark" });
	}
	
	/**
	 * 调整后的节点
	 * @param response
	 * @param planNodeAdjust
	 * @param pageIndex
	 * @param pageSize
	 */
	@RequestMapping("/adjustListByPage")
	@ResponseBody
	public void adjustListByPage(HttpServletResponse response,String yearMonth, PlanNodeAdjust planNodeAdjust, int pageIndex, int pageSize) {
		Pagination<PlanNodeAdjust> pagination = planNodeAdjustService.listByPage(yearMonth,planNodeAdjust, pageIndex, pageSize);
		jsonHandler.sendJsonString(response, true, "获取数据成功", pagination,
				new String[] {"id","planNode.id","planNode.plan.name", "nodeName", "planNode.business.id", "planNode.business.typeName", "planNode.nodeProperty.id", "planNode.nodeProperty.value", "beginDate", "endDate", "adjustDeadline", "auditDate", "auditResult",
						"auditState","delayDate","adjustType.id","adjustType.value","remark","createDate","createBy" },
				new String[] {"id","planNodeId","planName","nodeName", "business", "businessName", "nodeProperty", "nodePropertyValue", "beginDate", "endDate", "adjustDeadline", "auditDate", "auditResult",
						"auditState","delayDate","adjustType","adjustTypeValue","remark","createDate","createBy"});
	}
	@Override
	@RequestMapping("/save")
	@ResponseBody
	public ResponseMessage save(PlanNodeAdjust t) {
		String message = planNodeAdjustService.save(t);
		if (message != null) {
			return ResponseMessage.createMessage(true, t.getId()+"");
		} else {
			return ResponseMessage.createMessage(false, message);
		}
	}

	@Override
	@RequestMapping("/edit")
	@ResponseBody
	public ResponseMessage edit(PlanNodeAdjust t) {
		PlanNodeAdjust planNodeAdjust=planNodeAdjustService.getById(t.getId());
		if(planNodeAdjust.getAuditState()==0) {
			planNodeAdjust.setAuditState(1);
		}
		String message = planNodeAdjustService.edit(planNodeAdjust);
		if (message == null) {
			return ResponseMessage.createMessage(true, "保存计划节点成功！");
		} else {
			return ResponseMessage.createMessage(false, message);
		}
	}

	@Override
	@RequestMapping("/del")
	@ResponseBody
	public ResponseMessage del(PlanNodeAdjust t) {

		PlanNodeAdjust planNodeAdjust=planNodeAdjustService.getById(t.getId());
		String message = planNodeAdjustService.del(planNodeAdjust);
		if (message != null) {
			return ResponseMessage.createMessage(true, "删除成功！");
		} else {
			return ResponseMessage.createMessage(false, message);
		}
	}

	@RequestMapping("/info")
	@ResponseBody
	public void info(HttpServletResponse response,long id) {
		PlanNodeAdjust planNodeAdjust = planNodeAdjustService.getById(id);
		

		jsonHandler.sendJsonString(response, true, "获取数据成功", planNodeAdjust,
				new String[] {"id","planNode.id","planNode.plan.name", "nodeName", "planNode.business.id", "planNode.business.typeName", "planNode.nodeProperty.id", "planNode.nodeProperty.value", "beginDate", "endDate", "adjustDeadline", "auditDate", "auditResult",
						"auditState","delayDate","adjustType.id","adjustType.value","remark","createDate","createBy" ,"adjustBeginDate","adjustEndDate" },
				new String[] {"id","planNodeId","planName","nodeName", "business", "businessName", "nodeProperty", "nodePropertyValue", "beginDate", "endDate", "adjustDeadline", "auditDate", "auditResult",
						"auditState","delayDate","adjustType","adjustTypeValue","remark","createDate","createBy","adjustBeginDate","adjustEndDate" });
	}

	/**
	 * 查询所有的计划节点
	 * 
	 * @author mics
	 * @date 2018年7月11日
	 * @version 1.0
	 */
	@RequestMapping("/listAll")
	@ResponseBody
	public List<PlanNode> listAll() {
		List<PlanNode> planNodes = planNodeService.queryBusiSysPlan();
		return planNodes;
	}


}
